/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hawk.demo;

import glz.hawkgenerator.java.ClassSpec;
import glz.hawkgenerator.java.InterfaceSpec;
import glz.hawkgenerator.java.MethodSpec;
import glz.hawkgenerator.java.type.*;
import hawk.util.ProjectUtils;

import javax.lang.model.element.Modifier;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * This class is responsible for
 *
 * @author Hawk
 */
public class GenericGenerator {
    private final String packageName;

    public GenericGenerator(String packageName) {
        this.packageName = packageName;
    }

    public void generateGeneric() {
        ProjectUtils.write(packageName, InterfaceSpec.builder("InterfaceB", Modifier.PUBLIC)
                .build());
        ProjectUtils.write(packageName, InterfaceSpec.builder("InterfaceC", Modifier.PUBLIC)
                .build());
        ProjectUtils.write(packageName, InterfaceSpec.builder("InterfaceA", Modifier.PUBLIC)
                .addTypeVariable(TypeVariableName.of("K"))
                .addTypeVariable(TypeVariableName.of("V", ClassName.of(packageName, "InterfaceB")))
                .addTypeVariable(TypeVariableName.of("O", ClassName.of(packageName, "InterfaceB"), ClassName.of(packageName, "InterfaceC")))
                .addMethod(MethodSpec.builder(TypeVariableName.of("K"), "convertTo")
                        .addParameter(TypeVariableName.of("V"), "v")
                        .addParameter(TypeVariableName.of("O"), "o")
                        .build())
                .build());

        ProjectUtils.write(packageName, InterfaceSpec.builder("InterfaceAA", Modifier.PUBLIC)
                .addTypeVariable(TypeVariableName.of("K"))
                .addTypeVariable(TypeVariableName.of("V", ClassName.of(packageName, "InterfaceB")))
                .addTypeVariable(TypeVariableName.of("O", ClassName.of(packageName, "InterfaceB"), ClassName.of(packageName, "InterfaceC")))
                .addSuperInterface(ParameterizedTypeName.of(ClassName.of(packageName, "InterfaceA"), TypeVariableName.of("K"), TypeVariableName.of("V"), TypeVariableName.of("O")))
                .build());

        ProjectUtils.write(packageName, ClassSpec.builder("TypeExample", Modifier.PUBLIC, Modifier.ABSTRACT)
                .addTypeVariable(TypeVariableName.of("K"))
                .addField(PrimitiveTypeName.DOUBLE, "d1", Modifier.STATIC)
                .addField(PrimitiveTypeName.FLOAT, "f1", Modifier.STATIC)
                .addField(LocalDateTime.class, "dateTime", Modifier.PUBLIC, Modifier.STATIC)
                .addField(ClassName.ofClass(LocalDate.class), "date")
                .addField(ClassName.ofGuess(LocalTime.class.getCanonicalName()), "time")
                .addField(ClassName.of("java.util", "Map", "Entry"), "entry")
                .addField(TypeVariableName.of("K"), "k", Modifier.PRIVATE)
                .addField(ParameterizedTypeName.of(List.class, TypeVariableName.of("K")), "ks", Modifier.PUBLIC)
                .addField(ParameterizedTypeName.of(List.class, WildcardTypeName.of()), "ks1", Modifier.PUBLIC)
                .addField(ParameterizedTypeName.of(List.class, WildcardTypeName.ofUpper(Serializable.class)), "ks2", Modifier.PUBLIC)
                .addField(ParameterizedTypeName.of(List.class, WildcardTypeName.ofLower(Serializable.class)), "ks3", Modifier.PUBLIC)
                .addField(ParameterizedTypeName.of(Map.class, ClassName.ofClass(String.class), WildcardTypeName.of()), "map1", Modifier.PUBLIC)
                .addField(ParameterizedTypeName.of(Map.class, ClassName.ofClass(String.class), ParameterizedTypeName.of(Map.class, ClassName.ofClass(Integer.class), WildcardTypeName.ofUpper(TypeVariableName.of("K")))), "map2", Modifier.PUBLIC)
                .addField(ArrayTypeName.ofTypeName(TypeVariableName.of("K")), "array1", Modifier.PUBLIC)
                .addField(PrimitiveTypeName.INT, "intA", Modifier.PUBLIC)
                .addField(Integer.class, "integerA", Modifier.PUBLIC)
                .addField(ArrayTypeName.ofTypeName(PrimitiveTypeName.INT), "intArray", Modifier.PUBLIC)
                .addField(ArrayTypeName.ofTypeName(ArrayTypeName.ofTypeName(PrimitiveTypeName.INT)), "intArrayArray", Modifier.PUBLIC)
                .addField(ArrayTypeName.ofClass(Integer.class), "integerArray", Modifier.PUBLIC)
                .addMethod(MethodSpec.builder(VoidTypeName.VOID, "method0", Modifier.PUBLIC, Modifier.ABSTRACT).build())
                .addMethod(MethodSpec.builder(TypeVariableName.of("H"), "method1", Modifier.PROTECTED, Modifier.ABSTRACT)
                        .addTypeVariable(TypeVariableName.of("H"))
                        .addParameter(TypeVariableName.of("K"), "k", Modifier.FINAL)
                        .build())
                .addMethod(MethodSpec.builder(TypeVariableName.of("H"), "method2", Modifier.PROTECTED, Modifier.ABSTRACT)
                        .addTypeVariable(TypeVariableName.of("H"))
                        .addTypeVariable(TypeVariableName.of("V"))
                        .addTypeVariable(TypeVariableName.of("E", Throwable.class))
                        .addParameter(TypeVariableName.of("K"), "k", Modifier.FINAL)
                        .addParameter(TypeVariableName.of("V"), "v", Modifier.FINAL)
                        .addThrowable(TypeVariableName.of("E"))
                        .build())
                .build());
    }
}
